﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;

public partial class CheckOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string account;
            HttpCookie usr = Request.Cookies["usr"];
            account = usr.Value;
            fullfillDate();
            showAllOrder(account);
        }
    }
    private void fullfillDate()
    {
        for (int i = 1995; i < 2018; ++i)
        {
            StartYearDrop.Items.Insert(i - 1995, new ListItem(i.ToString(), i.ToString()));
        }
        StartYearDrop.SelectedIndex = 2017 - 1995;
        for (int i = 1; i < 13; ++i)
        {
            StartMonthDrop.Items.Insert(i - 1, new ListItem(i.ToString(), i.ToString()));
        }
        StartMonthDrop.SelectedIndex = 1 - 1;

        StartYearDrop.SelectedIndex = 2017 - 1995;
        for (int i = 1; i < 32; ++i)
        {
            StartDayDrop.Items.Insert(i - 1, new ListItem(i.ToString(), i.ToString()));
        }
        StartDayDrop.SelectedIndex = 1 - 1;

        for (int i = 1995; i < 2018; ++i)
        {
            EndYearDrop.Items.Insert(i - 1995, new ListItem(i.ToString(), i.ToString()));
        }
        EndYearDrop.SelectedIndex = 2017 - 1995;
        for (int i = 1; i < 13; ++i)
        {
            EndMonthDrop.Items.Insert(i - 1, new ListItem(i.ToString(), i.ToString()));
        }
        EndMonthDrop.SelectedIndex = 1 - 1;

        EndYearDrop.SelectedIndex = 2017 - 1995;
        for (int i = 1; i < 32; ++i)
        {
            EndDayDrop.Items.Insert(i - 1, new ListItem(i.ToString(), i.ToString()));
        }
        EndDayDrop.SelectedIndex = 1 - 1;
        return;
    }
    private void showOrder(string account,string startDate , string endDate)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;
        List<string> OnoList = new List<string>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            //先通过Aaccount找到所有订单号Dno日期作为约束条件
            //再根据Dno，找到所有信息。
            try
            {
                conn.Open();
                String sqlString = "select Ono from Customer_Set_order where Aaccount = @username and SOtime >= @startDate and SOtime <= @endDate";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@username", account),                                                
                                                new SqlParameter("@startDate", startDate),                                                
                                                new SqlParameter("@endDate", endDate)
                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            OnoList.Add(dr["Ono"].ToString());
                        }

                    }
                    dr.Close();
                }
                //再根据Dno，找到所有信息。
                foreach (var item in OnoList)
                {
                    //填写订单信息
                    string Ono = item;
                    sqlString = "select * from Customer_ViewOrderHist where Ono = @Ono";
                    SqlParameter[] tempParameters = { 
                                                        new SqlParameter("@Ono", Ono)
                                                    };
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sqlString;
                        cmd.Parameters.AddRange(tempParameters);
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr != null)
                        {
                            Table table = new Table();
                            double sum = 0;
                            string Osumprice ;
                            string AvipLel;
                            dr.Read();

                            //填写订单信息
                            fillOrderInf(dr, table);
                            TablePlaceHolder.Controls.Add(table);
                            Osumprice=dr["Osumprice"].ToString();
                            AvipLel = dr["AvipLel"].ToString();
                            //填写菜单信息
                            do
                            {
                                sum += fillDishInf(dr, table);
                            } while (dr.Read());
                            //填写总计
                            TableRow row = new TableRow();
                            row = new TableRow();
                            AddCell(row, " ");
                            AddCell(row, " ");
                            AddCell(row, "总计");
                            AddCell(row, Osumprice);
                            AddCell(row, "VIP等级：");
                            AddCell(row, AvipLel);
                            table.Rows.Add(row);
                        }
                        dr.Close();
                    }
                }
            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }

    }
    private void showAllOrder(string account)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;
        List<string> OnoList = new List<string>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            //先通过Aaccount找到所有订单号Dno日期作为约束条件
            //再根据Dno，找到所有信息。
            try
            {
                conn.Open();
                String sqlString = "select Ono from Customer_Set_order where Aaccount = @username";
                SqlParameter[] parameters = { new SqlParameter("@username", account) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            OnoList.Add(dr["Ono"].ToString());
                        }

                    }
                    dr.Close();
                }
                //再根据Dno，找到所有信息。
                foreach (var item in OnoList)
                {
                    //填写订单信息
                    string Ono = item;
                    sqlString = "select * from Customer_ViewOrderHist where Ono = @Ono";
                    SqlParameter[] tempParameters = { 
                                                        new SqlParameter("@Ono", Ono)
                                                    };
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sqlString;
                        cmd.Parameters.AddRange(tempParameters);
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr != null)
                        {
                            Table table = new Table();
                            table.Style.Add(HtmlTextWriterStyle.MarginTop, "40px");
                            double sum = 0;
                            string Osumprice ;
                            string AvipLel;
                            dr.Read();

                            //填写订单信息
                            fillOrderInf(dr, table);
                            TablePlaceHolder.Controls.Add(table);
                            Osumprice=dr["Osumprice"].ToString();
                            AvipLel = dr["AvipLel"].ToString();
                            //填写菜单信息
                            do
                            {
                                sum += fillDishInf(dr, table);
                            } while (dr.Read());
                            //填写总计
                            TableRow row = new TableRow();
                            row = new TableRow();
                            AddCell(row, "原价");
                            AddCell(row, sum.ToString()+"￥");
                            AddCell(row, "会员折后价");
                            AddCell(row, Osumprice);
                            AddCell(row, "VIP等级：");
                            AddCell(row, AvipLel);
                            table.Rows.Add(row);
                        }
                        dr.Close();
                    }
                    //填写菜式信息


                }
            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private void AddCell(TableRow row, string str)
    {
        TableCell cell = new TableCell();
        Label lab = new Label();
        lab.Text = str;
        cell.Width = new Unit(100);
        cell.Controls.Add(lab);
        row.Cells.Add(cell);
    }
    private void fillOrderInf(SqlDataReader dr, Table OrderListTable)
    {
        TableRow row = new TableRow();
        //第一行
        row = new TableRow();
        //订单号
        AddCell(row, "订单号：");
        AddCell(row, dr["Ono"].ToString());
        //下单时间
        AddCell(row, "下单时间：");
        AddCell(row, dr["SOtime"].ToString());

        OrderListTable.Rows.Add(row);

        //第二行
        row = new TableRow();
        //收餐人姓名
        AddCell(row, "收餐人姓名：");
        AddCell(row, dr["Oname"].ToString());
        //收餐联系方式
        AddCell(row, "收餐联系方式：");
        AddCell(row, dr["Ophone"].ToString());

        OrderListTable.Rows.Add(row);

        //第三行
        row = new TableRow();
        //收餐人地址
        AddCell(row, "收餐人地址：");
        AddCell(row, dr["Oadr"].ToString());
        //收餐联系方式
        AddCell(row, "订餐留言：");
        AddCell(row, dr["SOremarks"].ToString());
        OrderListTable.Rows.Add(row);

        //第四行
        AddCell(row, "订单状态");
        string state = dr["Ostate"].ToString();

        AddCell(row, state);
        if (state == "已下订" )
        {
            HyperLink link = new HyperLink();
            link.Text = "退单";
            link.NavigateUrl = "./Unset.aspx?Ono=" + dr["Ono"].ToString();
            TableCell cell = new TableCell();
            cell.Controls.Add(link);
            row.Cells.Add(cell);


        }
        else if(state == "已接单")
        {

        }
        else if (state == "正在派送")
        {
            AddCell(row, "派送员编号：");
            AddCell(row, dr["Sno"].ToString());
        }
        else if (state == "完成交易")
        {
            AddCell(row, "派送员编号：");
            AddCell(row, dr["Sno"].ToString());
        }
        else if (state == "申请退单")
        {
            AddCell(row, "退单原因：");
            AddCell(row, dr["USreason"].ToString());
        }
        else if (state == "已退单")
        {
            OrderListTable.Rows.Add(row);
            row = new TableRow();
            AddCell(row, "退单时间：");
            AddCell(row, dr["UStime"].ToString());
            AddCell(row, "退单原因：");
            AddCell(row, dr["USreason"].ToString());
        }
        OrderListTable.Rows.Add(row);

    }
    private double fillDishInf(SqlDataReader dr, Table OrderListTable)
    {
        TableRow row = new TableRow();
        double sum =0;
        row = new TableRow();
        AddCell(row, dr["Dname"].ToString());
        AddCell(row, dr["Dprice"].ToString()+"￥");
        AddCell(row, "×" + dr["Cnum"].ToString());
        sum = double.Parse(dr["Dprice"].ToString())*int.Parse( dr["Cnum"].ToString());
        AddCell(row, "=  " + sum.ToString() + "￥");
        OrderListTable.Rows.Add(row);
        return sum;
    }
    protected void SearchAllBut_Click(object sender, EventArgs e)
    {
        string account;
        HttpCookie usr = Request.Cookies["usr"];
        account = usr.Value;
        fullfillDate();
        showAllOrder(account);
    }
    protected void SearchBut_Click(object sender, EventArgs e)
    {
        string startDate;
        string endDate;
        startDate = StartYearDrop.SelectedValue.ToString() + "-" + StartMonthDrop.SelectedValue.ToString() + "-" + StartDayDrop.SelectedValue.ToString() + " 00:00:00";
        endDate = EndYearDrop.SelectedValue.ToString() + "-" + EndMonthDrop.SelectedValue.ToString() + "-" + EndDayDrop.SelectedValue.ToString() + " 23:59:59"; ;
       
        string account;
        HttpCookie usr = Request.Cookies["usr"];
        account = usr.Value;
        fullfillDate();
        showOrder(account, startDate, endDate);
    }
    protected void But_Click(object sender, CommandEventArgs e)
    {
        string no = e.CommandArgument.ToString();
        Response.Redirect("./Unset.aspx?Ono=" + no);
    }
}